<script lang="ts" setup>
import { review } from "../service";
import { IResponseData } from "@/@types/utils.request";
import { formParams } from "../data";

const props = defineProps({
	modelValue: {
		type: Boolean,
		default: false,
	},
	editList: {
		type: Object,
		default: () => ({}),
	},
});

const emit = defineEmits<{
	(e: "update:modelValue", value: boolean): void;
	(e: "refresh", value: boolean): void;
}>();
const visible = computed({
	get() {
		return props.modelValue;
	},
	set(val) {
		emit("update:modelValue", !!val);
	},
});

const editData = ref<formParams>({
	examineRemark: "",
	examineStatus: "",
	doctorType: 1,
	doctorId: 0,
});

watch(
	() => props.editList,
	(newVal) => {
		editData.value = {
			examineRemark: newVal.examineRemark,
			examineStatus: newVal.examineStatus,
			doctorType: newVal.doctorType,
			doctorId: newVal.doctorId,
		} as formParams;
	},
);
const dType = [
	{
		id: 1,
		name: "医生",
	},
	{
		id: 2,
		name: "营养师",
	},
];

const statusList = [
	{
		id: 1,
		name: "通过",
	},
	{
		id: 3,
		name: "不通过",
	},
];
const formRef = ref<any>();
const onConfirmm = async () => {
	try {
		const response: IResponseData<any> = await review({
			...editData.value,
		});
		const { code, msg } = response;
		if (code === 0) {
			ElMessage({
				message: msg,
				type: "success",
			});
			formRef.value.resetFields();
			emit("refresh", true);
			emit("update:modelValue", false);
		} else {
			ElMessage({
				message: msg,
				type: "error",
			});
		}
	} catch (error: any) {
		console.log(error);
	}
};

const onCancel = () => {
	ElMessageBox.confirm("您所编辑的内容还未保存，此时退出会导致修改内容失效，确定要退出吗？", {
		confirmButtonText: "确认",
		cancelButtonText: "取消",
		type: "warning",
	}).then(() => {
		formRef.value.resetFields();
		emit("update:modelValue", false);
	});
};

const rules = ref({
	doctorType: [{ required: true, message: "请选择", trigger: "change" }],
	examineStatus: [{ required: true, message: "请选择", trigger: "change" }],
});
</script>

<template>
	<el-dialog
		v-model="visible"
		title="审核"
		:rules="rules"
		:close-on-press-escape="false"
		width="520px"
		:before-close="onCancel"
	>
		<el-form :model="editData" label-width="auto" ref="formRef">
			<el-form-item label="类型：" prop="doctorType ">
				<el-select v-model="editData.doctorType" placeholder="请选择">
					<el-option v-for="item in dType" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="审核状态：" prop="examineStatus ">
				<el-select v-model="editData.examineStatus" placeholder="请选择">
					<el-option v-for="item in statusList" :key="item.id" :label="item.name" :value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="备注：" prop="auditFailCause ">
				<el-input v-model="editData.examineRemark" :rows="2" type="textarea" placeholder="请输入..." />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="onCancel">取消</el-button>
				<el-button type="primary" @click="onConfirmm"> 确认 </el-button>
			</span>
		</template>
	</el-dialog>
</template>
